//@flow
/**
 * 版本页面
 */
import React, { Component } from 'react';
import { 
    View,
    Image,
    Text,
    ScrollView,
    StyleSheet,
 } from 'react-native';
import CodePush from "react-native-code-push";
import { NavBar } from '../../container/navBar';
import { 
    List,
 } from "antd-mobile-rn";

type State = {
    progress : number,
    label : Array<string>,
    current:string,
}
class Version extends Component <{},State>{
    /**初始化 */
    state : State = {
        label:['正在初始化'],
        progress:0,
        current:'1.0.0.0',
    }
    componentDidMount(){
        //测试使用峰会production key获取元数据信息
        CodePush.checkForUpdate().then(rPackage=>{
            if(rPackage){
                console.log(`远程包下载地址:${rPackage.downloadUrl}`)
                console.log('开始下载包')
                rPackage.download((progress)=>{
                    console.log(`下载进度：${process.receivedBytes/progress.totalBytes}`)
                }).then(lPackage=>{
                    console.log(`下载完成,即将安装`)
                    lPackage.install(CodePush.InstallMode.IMMEDIATE).then(()=>{
                        console.log('安装完成，即将重启')
                    })
                })
            }else{
                console.log('没有发现远程包')
            }
        })
        CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING).then(info=>{
            this.setState({current:JSON.stringify(info)})
            console.log(`本地版本`+info.appVersion)
            console.log(`local package info : ${JSON.stringify(info)}`)
        })
    }
    // CodePushStatusDidChange(status){
    //     switch(status) {
    //         case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
    //             console.log("Checking for updates.");
    //             this.changeStatus('\n正在检查更新')
    //             break;
    //         case CodePush.SyncStatus.DOWNLOADING_PACKAGE:
    //             console.log("Downloading package.");
    //             this.changeStatus('\n正在下载更新包')
    //             break;
    //         case CodePush.SyncStatus.INSTALLING_UPDATE:
    //             console.log("Installing update.");
    //             this.changeStatus('\n正在安装')
    //             break;
    //         case CodePush.SyncStatus.UP_TO_DATE:
    //             console.log("Up-to-date.");
    //             this.changeStatus('\n已经是最新了')
    //             break;
    //         case CodePush.SyncStatus.UPDATE_INSTALLED:
    //             console.log("Update installed.");
    //             this.changeStatus('\n更新版已安装完成')
    //             break;
    //     }
        
    // }
    /**修改文字状态 */
    changeStatus(newText:string){
        let label:Array<string> = [...this.state.label].push(newText);
        this.setState({label})
    }
    // CodePushDownloadDidProgress(progress) {
    //     console.log(progress.receivedBytes + " of " + progress.totalBytes + " received.");
    //     this.setState({progress:progress.receivedBytes/progress.totalBytes})
    // }
    render(){
        return (
            <View>
                <NavBar titleView="版本检查"/>
                <View>
                    <Text>
                        {this.state.label.map(item=>{
                            return item+'\n'
                        })}
                    </Text>
                    <Text>
                        下载进度:{this.state.progress}
                    </Text>
                    <Text>
                        当前版本:{this.state.current}
                    </Text>
                </View>
            </View>
        )
    }
}
Version = CodePush({
    checkFrequency:CodePush.CheckFrequency.ON_APP_RESUME|CodePush.CheckFrequency.MANUAL,
    installMode:CodePush.InstallMode.ON_APP_RESUME,
    mandatoryInstallMode:CodePush.InstallMode.ON_APP_RESUME,
})(Version)
export {Version}